﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HR.ProjectX.DAL.UnitOfWork;
using NHibernate;
using HR.ProjectX.DAL.SessionStorage;
using NHibernate.Criterion;
using HR.ProjectX.Models.Entities.Vehicles;

namespace HR.ProjectX.DAL.Repositories
{
    public class VehicleRepository : Repository<Vehicle, int>, IVehicleRepository
    {
        public VehicleRepository(IUnitOfWork uow)
            : base(uow)
        {
        }

        public IList<Vehicle> FindAllVehiclesByCategory(string vehicleCategory)
        {
            ICriteria criteriaQuery = SessionFactory.GetCurrentSession()
                    .CreateCriteria(typeof(Vehicle))
                    .CreateAlias("VehicleCategory", "VehicleCategory")
                    .Add(NHibernate.Criterion.Restrictions.Eq("VehicleCategory.VehicleCategoryDescription", vehicleCategory));

            return (List<Vehicle>)criteriaQuery.List<Vehicle>();
        }

        public Vehicle FindVehicleByModel(string vehicleModel)
        {
            ICriteria criteriaQuery = SessionFactory.GetCurrentSession()
                    .CreateCriteria(typeof(Vehicle))
                    .CreateAlias("Manufacturer", "Manufacturer")
                    .CreateAlias("Model", "Model")
                    .CreateAlias("VehicleCategory", "VehicleCategory")
                    .Add(NHibernate.Criterion.Restrictions.Eq("Model.ModelName", vehicleModel));
            
            return (Vehicle)criteriaQuery.List<Vehicle>().FirstOrDefault();
        }

        

        
    }
}
